/**
 * 自定义日期时间选择器扩展
 * 支持限制特定日期或周几的选择
 * 基于eonasdan-bootstrap-datetimepicker
 */

(function($) {
    'use strict';

    // 添加自定义样式
    if (!$('#custom-datetimepicker-styles').length) {
        $('<style id="custom-datetimepicker-styles">')
            .text(`
                /* 可选择日期的绿色样式 */
                .bootstrap-datetimepicker-widget .datepicker table td.day:not(.disabled):not(.old):not(.new) {
                    background-color: #d4edda !important;
                    color: #155724 !important;
                    border: 1px solid #c3e6cb !important;
                    font-weight: bold;
                }
                
                .bootstrap-datetimepicker-widget .datepicker table td.day:not(.disabled):not(.old):not(.new):hover {
                    background-color: #28a745 !important;
                    color: white !important;
                    border-color: #1e7e34 !important;
                }
                
                .bootstrap-datetimepicker-widget .datepicker table td.active.day {
                    background-color: #28a745 !important;
                    color: white !important;
                    border-color: #1e7e34 !important;
                }
                
                .bootstrap-datetimepicker-widget .datepicker table td.today.day:not(.disabled) {
                    background-color: #ffc107 !important;
                    color: #212529 !important;
                    border-color: #ffc107 !important;
                    font-weight: bold;
                }
                
                .bootstrap-datetimepicker-widget .datepicker table td.today.day:not(.disabled):hover {
                    background-color: #e0a800 !important;
                    color: white !important;
                }
                
                /* 禁用日期的灰色样式 */
                .bootstrap-datetimepicker-widget .datepicker table td.disabled {
                    background-color: #f8f9fa !important;
                    color: #6c757d !important;
                    border-color: #dee2e6 !important;
                }
            `)
            .appendTo('head');
    }

    // 扩展jQuery方法
    $.fn.restrictedDatetimepicker = function(options) {
        var defaults = {
            format: 'YYYY-MM-DD',
            locale: 'zh-cn',
            icons: {
                time: 'fa fa-clock-o',
                date: 'fa fa-calendar',
                up: 'fa fa-chevron-up',
                down: 'fa fa-chevron-down',
                previous: 'fa fa-chevron-left',
                next: 'fa fa-chevron-right',
                today: 'fa fa-history',
                clear: 'fa fa-trash',
                close: 'fa fa-remove'
            },
            showTodayButton: true,
            showClose: true
        };

        // 提取自定义选项
        var customOptions = {
            allowedDates: options.allowedDates || null,
            allowedWeekdays: options.allowedWeekdays || null,
            disabledDates: options.disabledDates || null,
            restrictionType: options.restrictionType || null
        };

        // 移除自定义选项，避免传递给datetimepicker
        var dtpOptions = $.extend({}, defaults, options);
        delete dtpOptions.allowedDates;
        delete dtpOptions.allowedWeekdays;
        delete dtpOptions.disabledDates;
        delete dtpOptions.restrictionType;

        return this.each(function() {
            var $element = $(this);
            
            // 根据限制类型设置禁用日期
            if (customOptions.restrictionType) {
                var disabledDates = [];
                var startDate = moment().subtract(1, 'month');
                var endDate = moment().add(2, 'years');
                
                // 生成所有日期，然后根据规则禁用
                var current = startDate.clone();
                while (current.isSameOrBefore(endDate)) {
                    var shouldDisable = false;
                    
                    if (customOptions.restrictionType === 'workdays') {
                        // 禁用周末 (周六=6, 周日=0)
                        shouldDisable = current.day() === 0 || current.day() === 6;
                    } else if (customOptions.restrictionType === 'weekends') {
                        // 禁用工作日 (周一到周五)
                        shouldDisable = current.day() >= 1 && current.day() <= 5;
                    } else if (customOptions.restrictionType === 'custom' && customOptions.allowedWeekdays) {
                        // 自定义周几，禁用不在允许列表中的日期
                        var dayOfWeek = current.isoWeekday(); // 1=周一, 7=周日
                        shouldDisable = customOptions.allowedWeekdays.indexOf(dayOfWeek) === -1;
                    } else if (customOptions.restrictionType === 'specific' && customOptions.allowedDates) {
                        // 指定日期，禁用不在允许列表中的日期
                        var dateStr = current.format('YYYY-MM-DD');
                        shouldDisable = customOptions.allowedDates.indexOf(dateStr) === -1;
                    } else if (customOptions.restrictionType === 'disabled' && customOptions.disabledDates) {
                        // 禁用特定日期，只禁用指定的日期
                        var dateStr = current.format('YYYY-MM-DD');
                        shouldDisable = customOptions.disabledDates.indexOf(dateStr) !== -1;
                    }
                    
                    if (shouldDisable) {
                        disabledDates.push(current.format('YYYY-MM-DD'));
                    }
                    
                    current.add(1, 'day');
                }
                
                // 设置禁用日期
                dtpOptions.disabledDates = disabledDates;
            }

            // 初始化datetimepicker
            $element.datetimepicker(dtpOptions);
        });
    };

    // 预设配置
    $.restrictedDatetimepicker = {
        // 只允许工作日（周一到周五）
        workdaysOnly: function(element, options) {
            var config = $.extend({
                restrictionType: 'workdays'
            }, options);
            return $(element).restrictedDatetimepicker(config);
        },

        // 只允许周末（周六、周日）
        weekendsOnly: function(element, options) {
            var config = $.extend({
                restrictionType: 'weekends'
            }, options);
            return $(element).restrictedDatetimepicker(config);
        },

        // 只允许特定周几
        customWeekdays: function(element, weekdays, options) {
            var config = $.extend({
                restrictionType: 'custom',
                allowedWeekdays: weekdays
            }, options);
            return $(element).restrictedDatetimepicker(config);
        },

        // 只允许特定日期
        specificDates: function(element, dates, options) {
            var config = $.extend({
                restrictionType: 'specific',
                allowedDates: dates
            }, options);
            return $(element).restrictedDatetimepicker(config);
        },

        // 禁用特定日期
        disableDates: function(element, dates, options) {
            var config = $.extend({
                restrictionType: 'disabled',
                disabledDates: dates
            }, options);
            return $(element).restrictedDatetimepicker(config);
        }
    };

})(jQuery);